package com.fengycx.mq.consumer;

import cn.hutool.core.util.ObjectUtil;
import com.fengycx.constants.Constants;
import com.fengycx.pojo.domain.Order;
import com.fengycx.service.IOrderService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 延时消息消费者
 * 用户呼叫代驾创建订单时，超60秒 自动取消订单
 */
@Component
@RocketMQMessageListener(
       consumerGroup = "order-delay-cancel-group",
        topic = "order-topic",
        selectorExpression = "delay-cancel-tag"
)
public class CancelOrderConsumer implements RocketMQListener<MessageExt> {


    @Autowired
    private IOrderService orderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String orderNo = new String(body, StandardCharsets.UTF_8);
        //判断是否为空，不为空则说明还没取消
        Order order = orderService.getById(orderNo);
        if(ObjectUtil.isNotNull(order) && Constants.Order.WAIT_ORDER.equals(order.getStatus())){
            orderService.cancelOrder(orderNo);
        }
    }


}
